var express=require("express");
var body=require("body-parser");
var request = require ("request");
var sha1 = require('./sha1');
var accesstoken = require('./accesstoken');
var sendmsg = require('./sendmsg');
var cronJob = require('cron').CronJob;
var xml2js = require('xml2js');
var app=express();
app.listen(8081);
console.log("8081")

app.use(express.static('public'));
app.use(body.urlencoded({extended:true})); //启动body;
var xmlParser = new xml2js.Parser({explicitArray : false, ignoreAttrs : true})

app.set("views","./views");//解析视图，创建视图位置
app.set("view engine","html");//视图解决方法
app.engine('html', require('ejs').renderFile); //将ejs转换成html
app.use("/static",express.static('public')); //添加static进行路径转换

//自己玩耍的搜狗表情
app.get('/',function(req,res){
    var arr =
        [
            'http://config.pinyin.sogou.com/picface/interface/query_zb.php?cands=5AsN5A626yo95zYO5y_r&tp=1&tid=-1&page=1&h=8F81A4722BB8B30D27D9A015F46427D8&v=8.6.0.1634&r=0000_sogoupinyin_8.6.0.1634&pv=6.1.7601&sdk=1.1.0.1766',
            'http://config.pinyin.sogou.com/picface/interface/query_zb.php?cands=5PYa6l_r5PU05PAW&tp=1&tid=-2&page=1&h=8F81A4722BB8B30D27D9A015F46427D8&v=8.6.0.1634&r=0000_sogoupinyin_8.6.0.1634&pv=6.1.7601&sdk=1.1.0.1766',
            'http://config.pinyin.sogou.com/picface/interface/query_zb.php?cands=5PAx5zU_5BQu562u&tp=1&tid=1033&page=1&h=8F81A4722BB8B30D27D9A015F46427D8&v=8.6.0.1634&r=0000_sogoupinyin_8.6.0.1634&pv=6.1.7601&sdk=1.1.0.1766',
            'http://config.pinyin.sogou.com/picface/interface/query_zb.php?cands=5Bcg5yAB5zYO5lIk&tp=1&tid=3610&page=1&h=8F81A4722BB8B30D27D9A015F46427D8&v=8.6.0.1634&r=0000_sogoupinyin_8.6.0.1634&pv=6.1.7601&sdk=1.1.0.1766',
            'http://config.pinyin.sogou.com/picface/interface/query_zb.php?cands=5OIr5lIn5PIV5PAx5zU_&tp=1&tid=2956&page=1&h=8F81A4722BB8B30D27D9A015F46427D8&v=8.6.0.1634&r=0000_sogoupinyin_8.6.0.1634&pv=6.1.7601&sdk=1.1.0.1766',
            'http://config.pinyin.sogou.com/picface/interface/query_zb.php?cands=5PEG5A2x5QIH5P2-&tp=1&tid=3200&page=1&h=8F81A4722BB8B30D27D9A015F46427D8&v=8.6.0.1634&r=0000_sogoupinyin_8.6.0.1634&pv=6.1.7601&sdk=1.1.0.1766',
            'http://config.pinyin.sogou.com/picface/interface/query_zb.php?cands=5yQO5Ocb5lIj6l_E&tp=1&tid=3602&page=1&h=8F81A4722BB8B30D27D9A015F46427D8&v=8.6.0.1634&r=0000_sogoupinyin_8.6.0.1634&pv=6.1.7601&sdk=1.1.0.1766',
        ];
    var arr1 = [];
    for (var i = 0;i<arr.length;i++){
        request(arr[i],function(err,res1,req){
            if(err){
                console.log()
            }else{
                var aaa = JSON.parse(res1.body).imglist;
                for(var j=0;j<aaa.length;j++){
                    arr1.push(aaa[j]);
                }
            }
        })
    }
    setTimeout(function(){
        res.render("doutu",{arr:arr1});
    },2000)
})


app.get('/weixin',function(req,res){
    var signature = req.query.signature;
    var timestamp = req.query.timestamp;
    var nonce = req.query.nonce;

    var token = 'boboweiqi93shihaibo';
    var arr = [];
    arr[0] = token;
    arr[1] = timestamp;
    arr[2] = nonce;
    arr = arr.sort();
    var str = arr[0]+arr[1]+arr[2];
    var tmpStr = sha1.sha1(str).toLowerCase();
    if(tmpStr == signature){
        res.send(req.query.echostr);
    }
})


app.get('/weixin',function(req,res){
    var signature = req.query.signature;
    var timestamp = req.query.timestamp;
    var nonce = req.query.nonce;

    var token = 'boboweiqi93shihaibo';
    var arr = [];
    arr[0] = token;
    arr[1] = timestamp;
    arr[2] = nonce;
    arr = arr.sort();
    var str = arr[0]+arr[1]+arr[2];
    var tmpStr = sha1.sha1(str).toLowerCase();
    if(tmpStr == signature){
        res.send(req.query.echostr);
    }
})

app.post('/weixin',function(req,res){
    console.log(req);
    var _da;
    req.on("data",function(data){
        _da = data.toString("utf-8");
    });
    req.on("end",function(){
        console.log(_da);
        xmlParser.parseString(_da, function (err, result) {
            if(result.xml.MsgType == 'event'){
                console.log('event');
            }else{
                console.log('other');
            }
        });
    })
})

new cronJob('0 */30 * * * *', function () {
    accesstoken();
}, null, true, 'Asia/Chongqing');

